package com.vladimirkolarevic.eventplanner.hibernate;

import java.io.Serializable;

import java.util.List;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.LockMode;
import org.hibernate.Session;




import com.vladimirkolarevic.eventplanner.Comment;
import com.vladimirkolarevic.eventplanner.Event;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.EventBean;
import com.vladimirkolarevic.eventplanner.service.EventsService;


//@ServiceId("Service")
public class EventsServiceHibernate implements EventsService{

	private final Session session;
	
	public EventsServiceHibernate(Session session) {
		this.session=session;
	}
	
	
	
	public Long addEvent(Event event) {
		
		Long id = (Long) session.save(event);
		
		return id;
	}


	public List<Event> retrive() {
		List<Event> events = session.createCriteria(EventBean.class).list();
		return events;
	}


	public Event retriveEvent(Serializable id) {
		// TODO Auto-generated method stub
		return (Event) session.get(EventBean.class, id);
	}
	
	
	
	public void update(Event event) {
		Event e = (Event) session.merge(event);
		session.update(e);
	}


	
	public void atach(Event event) {
		session.lock(event, LockMode.NONE);
		
	}



	public void delete(Comment comment) {
	// Comment c = (Comment) session.merge(comment);
		session.delete(comment);
		
	}



	public void delete(User user) {
		session.delete(user);
		
	}
	
	

}
